/**
 * 递归扁平化数组
 *
 * @param {Array} values
 * @returns Array
 */
export function arrayDeepFlat(values) {
  if (!Array.isArray(values)) {
    return [];
  }
  const data = [];
  values.forEach((value) => {
    if (Array.isArray(value.children)) {
      data.push(...arrayDeepFlat(value.children));
    }
    data.push(value);
  });
  return data;
}

/**
 * 数组并集
 *
 * @param {Array} arr1
 * @param {Array} arr2
 * @returns Array
 */
export const arrayUnion = (arr1, arr2) => Array.from(new Set([...arr1, ...arr2]));

/**
 * 数组交集
 *
 * @param {Array} arr1
 * @param {Array} arr2
 * @returns Array
 */
export const arrayCross = (arr1, arr2) => Array.from(new Set(arr1.filter((x) => arr2.includes(x))));

/**
 * 数组差集
 *
 * @param {Array} arr1
 * @param {Array} arr2
 * @returns Array
 */
export const arrayDiff = (arr1, arr2) => {
  const union = arrayUnion(arr1, arr2);
  const cross = arrayCross(arr1, arr2);
  return Array.from(new Set(union.filter((x) => !cross.includes(x))));
};

/**
 * 数组过滤
 *
 * @param {Array} arr1
 * @param {Array} arr2
 * @returns {Array} arr1 not in arr2
 */
export const arrayFilter = (arr1, arr2) => {
  return arr1.filter((x) => !arr2.includes(x));
};
